﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class IdocumentCodeApp : IIdocumentCode
    {
        private string dbname = "Systems";

        public void Add(IdocumentCodeEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_IdocumentCode(");
            builder.Append("CodeName,CodeNum,CodeRefName,CodeMaxValue,UpdateMan,UpdateTime");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.CodeName + "',");
            builder.Append("'" + model.CodeNum + "',");
            builder.Append("'" + model.CodeRefName + "',");
            builder.Append("" + model.CodeMaxValue + ",");
            builder.Append("'" + model.UpdateMan + "',");
            builder.Append("'" + model.UpdateTime + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int CodeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete T_IdocumentCode ");
            builder.Append(" where CodeId=" + CodeId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_IdocumentCode ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by CodeId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "CodeId", "T_IdocumentCode", swhere);
        }

        public int GetMaxValue(string codeRefName)
        {
            int num = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select CodeMaxValue,UpdateTime from T_IdocumentCode ");
            builder.Append(" where CodeRefName='" + codeRefName + "'");
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                if (DateTime.Parse(table.Rows[0]["UpdateTime"].ToString()).Day.ToString() != DateTime.Now.Day.ToString())
                {
                    this.UpdateMaxValueTwo(codeRefName);
                    return 1;
                }
                num = int.Parse(table.Rows[0][0].ToString()) + 1;
                this.UpdateMaxValue(codeRefName);
                return num;
            }
            return 0;
        }

        public IdocumentCodeEnt GetModel(int CodeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CodeId,CodeName,CodeNum,CodeRefName,CodeMaxValue,UpdateMan,UpdateTime from T_IdocumentCode ");
            builder.Append(" where CodeId=" + CodeId);
            IdocumentCodeEnt ent = new IdocumentCodeEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.CodeId = CodeId;
            if (table.Rows.Count > 0)
            {
                ent.CodeName = table.Rows[0]["CodeName"].ToString();
                ent.CodeNum = table.Rows[0]["CodeNum"].ToString();
                ent.CodeRefName = table.Rows[0]["CodeRefName"].ToString();
                if (table.Rows[0]["CodeMaxValue"].ToString() != "")
                {
                    ent.CodeMaxValue = int.Parse(table.Rows[0]["CodeMaxValue"].ToString());
                }
                ent.UpdateMan = int.Parse(table.Rows[0]["UpdateMan"].ToString());
                if (table.Rows[0]["UpdateTime"].ToString() != "")
                {
                    ent.UpdateTime = DateTime.Parse(table.Rows[0]["UpdateTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public IdocumentCodeEnt GetModel(string codeRefName)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CodeId,CodeName,CodeNum,CodeRefName,CodeMaxValue,UpdateMan,UpdateTime from T_IdocumentCode ");
            builder.Append(" where CodeRefName='" + codeRefName + "'");
            IdocumentCodeEnt ent = new IdocumentCodeEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["CodeId"].ToString() != "")
                {
                    ent.CodeId = int.Parse(table.Rows[0]["CodeId"].ToString());
                }
                ent.CodeName = table.Rows[0]["CodeName"].ToString();
                ent.CodeNum = table.Rows[0]["CodeNum"].ToString();
                ent.CodeRefName = table.Rows[0]["CodeRefName"].ToString();
                if (table.Rows[0]["CodeMaxValue"].ToString() != "")
                {
                    ent.CodeMaxValue = int.Parse(table.Rows[0]["CodeMaxValue"].ToString());
                }
                if (table.Rows[0]["UpdateMan"].ToString() != "")
                {
                    ent.UpdateMan = int.Parse(table.Rows[0]["UpdateMan"].ToString());
                }
                if (table.Rows[0]["UpdateTime"].ToString() != "")
                {
                    ent.UpdateTime = DateTime.Parse(table.Rows[0]["UpdateTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_IdocumentCode ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "CodeId");
        }

        public void Update(IdocumentCodeEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_IdocumentCode set ");
            builder.Append("CodeName='" + model.CodeName + "',");
            builder.Append("CodeNum='" + model.CodeNum + "',");
            builder.Append("CodeRefName='" + model.CodeRefName + "',");
            builder.Append("CodeMaxValue=" + model.CodeMaxValue + ",");
            builder.Append("UpdateMan='" + model.UpdateMan + "',");
            builder.Append("UpdateTime='" + model.UpdateTime + "'");
            builder.Append(" where CodeId=" + model.CodeId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void UpdateMaxValue(string codeRefName)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_IdocumentCode set ");
            builder.Append("CodeMaxValue=CodeMaxValue+1 ,");
            builder.Append("UpdateTime='" + DateTime.Now.ToString() + "'");
            builder.Append(" where CodeRefName='" + codeRefName + "'");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void UpdateMaxValueTwo(string codeRefName)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_IdocumentCode set ");
            builder.Append("CodeMaxValue=1,");
            builder.Append("UpdateTime='" + DateTime.Now.ToString() + "'");
            builder.Append(" where CodeRefName='" + codeRefName + "'");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

